#ifndef INICONFIG_H
#define INICONFIG_H

#include <QSettings>

class INIConfig : public QObject
{
    Q_OBJECT

public:
    static bool isIPValid(QString &ip);
    static bool safeVoluation(int &dest, int &src);
    static bool safeVoluation(int &dest, int &src, int min);
    static bool safeVoluation(int &dest, int &src, int min, int max);
    static bool safeVoluation(QString &dest, QString &src);
    static bool safeVoluation(QString &dest, QString &src, int maxLength);

    explicit INIConfig(QObject *parent = 0);
    ~INIConfig();

    bool initValue(const char *inipath);
    void dumpAll(void);

    // [Info]
    int myid;

    // [Code]
    int codemaxlen;

    // [Network]
    QString ifname;
    QString staticip;
    QString staticnetmask;
    QString staticgatway;
    QString dns1;
    QString dns2;

    // [MySQL]
    QString serverip;
    int serverport;
    QString databasename;
    QString username;
    QString password;
    QString farmertable;
    QString vegetable;
    QString logtable;
    QString datatable;

    // [Time]
    int synctimeout;

    // [Serial]
    QString serialpath;
    int serialrate;
    int serialdatabit;
    int serialstopbit;
    QString serialseparate;
    int serialdatavalidlen;
    int serialreadinterval;
    int serialbufferlen;


private:
    bool getValue(int &arg, const char *key);
    bool getValue(int &arg, const char *key, int min);
    bool getValue(int &arg, const char *key, int min, int max);
    bool getValue(QString &arg, const char *key);
    bool getValue(QString &arg, const char *key, int maxLength);
    bool getValue(QString &arg, const char *key, bool isIP);

    QSettings *inifile;
};

#endif // INICONFIG_H
